#include "glare.d3d11.SamplerState.h"
#include "glare.d3d11.DrawStateFactory.h"
#include "glare.d3d11.Driver.h"

namespace Glare {

	D3D11SamplerState::D3D11SamplerState(D3D11DrawStateFactory* factory, const SamplerStateDescription& description)
		: SamplerState(description)
		, _factory(factory)
		, _d3d_sampler_state(NULL)
	{
		D3D11Driver* d3d11_rs = static_cast<D3D11Driver*>(factory->GetDriver());

		D3D11_SAMPLER_DESC d3d11_sampler_desc;
		d3d11_sampler_desc.Filter			= (D3D11_FILTER)description.Filter;
		d3d11_sampler_desc.AddressU			= (D3D11_TEXTURE_ADDRESS_MODE)description.AddressU;
		d3d11_sampler_desc.AddressV			= (D3D11_TEXTURE_ADDRESS_MODE)description.AddressV;
		d3d11_sampler_desc.AddressW			= (D3D11_TEXTURE_ADDRESS_MODE)description.AddressW;
		d3d11_sampler_desc.MipLODBias		= description.MipLODBias;
		d3d11_sampler_desc.MaxAnisotropy	= description.MaxAnisotropy;
		d3d11_sampler_desc.ComparisonFunc	= (D3D11_COMPARISON_FUNC)description.ComparisonFunction;
		d3d11_sampler_desc.MinLOD			= description.MinLOD;
		d3d11_sampler_desc.MaxLOD			= description.MaxLOD;

		GLARE_THROW(d3d11_rs->GetD3DDevice()->CreateSamplerState(&d3d11_sampler_desc, &_d3d_sampler_state), "Error while creating D3D sampler state");
	}

	D3D11SamplerState::~D3D11SamplerState()
	{
		GLARE_SAFE_RELEASE(_d3d_sampler_state, "Error while releasing D3D sampler state");
	}

	DrawStateFactory* D3D11SamplerState::GetFactory() const
	{
		return _factory;
	}

	ID3D11SamplerState* D3D11SamplerState::GetD3DSamplerState() const
	{
		return _d3d_sampler_state;
	}

	D3D11DrawStateFactory* D3D11SamplerState::GetD3DFactory() const
	{
		return _factory;
	}
}
